package mails.da.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JdbcDAOFactory
 * 
 * has role as providing the Jdbc DAOs for the Factory
 * 
 * also as BaseJdbc
 * provides con,stmt,rs,log
 * methods for:	connection to the DB
 * 				executing of Queries
 * 				executing of Updates
 * 
 *
 * @author rob
 */
public class JdbcDAOFactory {
	
	Connection con;

	Statement stmt;

	ResultSet rs;
	
	
	private String path;

	public static final String driverName = "org.sqlite.JDBC";

	public static final String dbURL = "jdbc:sqlite:";
	
	public JdbcDAOFactory() {
		this.path = "";
	}
	
	public JdbcDAOFactory(String path) {
		this.path = path;
	}
	
	protected Connection createConnection() {
		try {
			Class.forName(driverName);
			return DriverManager.getConnection(dbURL+path);
		} catch(Exception ex){
			System.err.println("ERROR@MailsDBConnector: sqlite connection failed --"+ex.getMessage());
			return null;
		}
	}
	
	protected ResultSet executeQuery (Connection con, String query)  throws SQLException {
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery (query);
		return rs;
	}
	
	protected void executeUpdate (Connection con, String statement)  throws SQLException {
		Statement stmt = con.createStatement();
		stmt.executeUpdate(statement);
	}
	
	public JdbcMailDAO getMailDAO() {
		return new JdbcMailDAO();
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
}
